<?

// 支付宝相关操作
class AlipayHandler{
    
    private $s=null; // 票付通内部接口
    private $orderInfo = null; // 订单信息
    private $ordernum = 0; // 订单号
    
    public function __construct($s,$ordernum){
        $this->s = $s;
        $this->ordernum = $ordernum;
    }
    // 设置平台订单号
    public function setOrderNum($ordernum){
        $this->ordernum = $ordernum;
    }
    
    /* 回调处理
     * trade_no 支付宝交易号
     * alipay_to_pft 是否支付给票付通
     **/ 
    public function notify($trade_no,$alipay_to_pft=false){
        
        $s = $this->s;
        $ordernum = $this->ordernum;// 平台订单号
        
        $orderInfo = $this->getOrderInfo($s,$ordernum);// 订单信息
        $total_fee = $this->total_fee($orderInfo);// 获取总价格
        
        $aid = (string)$orderInfo->Rec[0]->UUaid;
        $mid = (string)$orderInfo->Rec[0]->UUmid;

        if($aid!=$mid){// 非自身供应须分润
            $aid_money = $this->fitDistribution($total_fee,$orderInfo,$alipay_to_pft);
            
        }else{// 支付给票付通需给分销商加上相应金额
            if($alipay_to_pft){
                // echo "PFT_Member_Fund_Modify($mid, 0, $total_fee, 0, 0,null,0, 0,$ordernum)";
                $s->PFT_Member_Fund_Modify($mid, 0, $total_fee, 0, 0,null,0, 0, $ordernum);
            }
        }
        
        // 平台支付宝交易记录变更已支付
        $str="update pft_alipay_rec set status=1,dtime=now(),`trade_no`='$trade_no' where out_trade_no='$ordernum' limit 1";
        $GLOBALS['le']->query($str);
        
         // 会员支付宝交易记录
        $str="insert pft_member_alipay set fid=$mid,orderid='$ordernum',total_fee=$total_fee,money='$aid_money',ali_account='$ali_account',rectime=now(),dtype='0'";
        $GLOBALS['le']->query($str);

        $reslut=$s->Mod_Order_Status($ordernum,1);
        $this->write_logs("[$ordernum]订单状态修改:$result");
        $s->reSend_SMS_Global_PL($ordernum,$aid,$mid);//最后重发短信
        
    }
    /*
     * 散客利润分配
     * $ordern 相关平台订单号
     * $aid 订单上级供应商 id
     * $mid 订单分销商 id
     * $total_fee 订单总金额
     * $orderInfo  订单相关数据
     * $to_pft 是否支付到票付通账号
     **/
    public function fitDistribution($total_fee,$orderInfo,$to_pft=false){
        
        $s = $this->s;
        $ordern = $this->ordernum;
        
        $aid = (string)$orderInfo->Rec[0]->UUaid;
        $mid = (string)$orderInfo->Rec[0]->UUmid;

        $add = 0; $cut = 1;
        
        // 非自供产品获取结算价
        if($aid!=$mid){
            $total_money = 0;
            foreach($orderInfo->Rec as $order){
                $pid = (string)$order->UUpid;// 产品 id
                $begintime = (string)$order->UUbegintime;// 游玩时间价格
                $account = (string)$order->UUfromt;// 分销商账号
                $ret = $s->Dynamic_Price_And_Storage($account,$pid,$begintime,1,0,0,$aid);
                if($ret==105||$ret==1065){
                    $total_money = $total_fee;
                }else{
                    $obj = simplexml_load_string($ret);
                    $total_money += (string)$obj->Rec->UUsprice * (int)$order->UUtnum;
                }
            }
        }
        
        // 支付给了票付通，购买者账户加钱
        if($to_pft) $s->PFT_Member_Fund_Modify($mid, 0, $total_fee, $add, 0,null,0, 0,$ordern);
        
        $s->PFT_Member_Fund_Modify($aid, 0, $total_money, $add, 0,null,0, 0,$ordern);// 给上级供应商加钱
        $s->PFT_Member_Fund_Modify($mid, 0, $total_money, $cut, 0,null,0, 0,$ordern);// 购买者账户扣钱
        
        // 产品多级销售分润
        $str="select aids_money from uu_order_fx_details where orderid='$ordern' limit 1";
        $GLOBALS['le']->query($str);
        $lirun = $GLOBALS['le']->fetch_assoc();
        if (!$lirun['aids_money']) return $total_money;// 返回上级利润
        
        // 分润操作
        if(count($orderInfo)>1){// 有子票，子票未记录分润
            $childMenoy = array();
            foreach($orderInfo->Rec as $order){
                if((string)$order->UUordernum==$ordern) continue;// 去掉主订单
                $aids = explode(',',(string)$order->UUaids);
                krsort($aids);
                $aids_price = explode(',',(string)$order->UUaids_price);
                krsort($aids_price);
                foreach($aids as $k=>$et){
                    if($k==0) break;// 最后一个不用操作了
                    $childMenoy[$et] = $aids_price[$k-1] * (string)$order->UUtnum;
                }
            }
        }

        // 主订单的 aids
        $main_aids = explode(',',(string)$orderInfo->Rec[0]->UUaids);
        $aids_money = unserialize($lirun['aids_money']);
        $opID = 0;
        foreach($aids_money as $row){
            $fid=$row[0];
            $action = ($row[2]==0) ? $cut : $add;//0减少,1增加和接口相反
            $Mmoney=$row[3];
            if($action==0){ // 加钱
                $ks = array_search($fid,$main_aids);
                $ks = $ks+1;
                $Mmoney += isset($childMenoy[$main_aids[$ks]]) ? $childMenoy[$main_aids[$ks]]:0;
            }else{
                $Mmoney += isset($childMenoy[$fid]) ? $childMenoy[$fid]:0;
            }
        
            if($row[1]==0){//信用额度
                $dmode = 1;
                $res = $s->PFT_Member_Fund_Modify($fid,$opID,$Mmoney,$action,$dmode,$row[4],0,2,$ordern);
            }else{//资金
                $dmode = 0;
                $res = $s->PFT_Member_Fund_Modify($fid,$opID,$Mmoney,$action,$dmode,NULL,0,0,$ordern);
            }
        }
        
        return $total_money;// 返回上级利润
    
    }
    
    
    // 获取订单总价格，以分为单位
    public function total_fee($orderInfo){
        
        $total_fee = 0;
        foreach($orderInfo->Rec as $order){
            $total_fee+=((string)$order->UUtprice * (string)$order->UUtnum);
        }
        return $total_fee;
    }
    
    /*
     * 获取订单信息
     * $s   票付通内部接口
     * $ordernum  主订单订单号
     **/
    public function getOrderInfo(){
        $s = $this->s;
        $ordernum = $this->ordernum;
		$xml=$s->Order_Globle_Search("","","","","","","","","","","","",$ordernum,"","","","","",1,1,0,1,"","","","","","","","","",0,1);
		$simxml = simplexml_load_string($xml);
        if($simxml->Rec->UUp_type=='F') return $simxml;
        
        // 如果 UUconcat_id 不为 0 表示复票
        $concatId = (string)$simxml->Rec->UUconcat_id;
        if($concatId!=0 && $concatId==(string)$simxml->Rec->UUordernum){
            $xml=$s->Order_Globle_Search("","","","","","","","","","","","",$ordernum,"","","","","",1,1,0,99,"","","","","","","","","",1,0);
        }
        
        $simxml = simplexml_load_string($xml);
        return $simxml;
	}
    
    public function write_logs($txt,$file="success_logs.txt"){
        $base_url = '/var/www/html/new/d/shops/alipay/ali_order/';
        $fp = fopen($file,"a");
        flock($fp, LOCK_EX);
        fwrite($fp,date("Y-m-d H:i:s").":".$txt."\n");
        flock($fp, LOCK_UN);
        fclose($fp);
    }

}

?>